package com.fengye.algorithms.leecode;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author fengye
 * @Date 2023/4/10 9:05
 **/
public class MatrixReshape566 {
    public int[][] matrixReshape(int[][] mat, int r, int c) {
        int mr = mat.length;
        int mc = mat[0].length;

        if(r*c != mr*mc) {
            return mat;
        }
        List<Integer> list = new ArrayList<>();
        for(int i=0;i<mr;i++) {
            for(int j=0;j<mc;j++) {
                list.add(mat[i][j]);
            }
        }

        int[][] newMatrix = new int[r][c];
        for(int i = 0;i < r;i++) {
            for (int j = 0; j < c;j++) {
                newMatrix[i][j] = list.get(i*c + j);
            }
        }

        return newMatrix;
    }

    public int[][] matrixReshape2(int[][] mat, int r, int c) {
        if (mat.length * mat[0].length != r * c) return mat;
        int[][] res = new int[r][c];
        for (int x = 0; x < mat.length * mat[0].length; x++)
            res[x / c][x % c] = mat[x / mat[0].length][x % mat[0].length];
        return res;
    }
}
